<?php
/**
 * ImpSoft Product Model
 * All functionality for the category goes in here.
 * 
 * @package ImpSoft
 * @subpackage ImpSoft Models
 * @copyright Luke Visinoni (luke.visinoni@gmail.com)
 * @author Luke Visinoni (luke.visinoni@gmail.com)
 * @license GNU Lesser General Public License
 */
class Product extends ImpSoft_Db_Table {

    protected $_name = 'products';
    protected $_primary = 'product_id';
    protected $_dependentTables = array();
    protected $_rowClass = 'ProductRow';
    protected $_required = array('name');
	/**
	 * Add any filters that should be applied to this model when saving
	 */
	public function getFilters() {
	
		$filters = new Q_Filter_Set();
		$filters->add(new Q_Filter_Float(), array('cost', 'price', 'weight'));
		return $filters;
	
	}
	/**
	 * Add any rules that should be applied to validation when saving
	 */
	public function getRules() {
	
		$rules = new Q_Rule_Set();
		$rules->add(new Q_Rule_NotNull(), array('title', /*'description',*/ 'cost', 'price', 'weight'), 'This is a required field')
			->add(new ImpSoft_ZendRule_Float(), array('cost', 'price', 'weight'), 'You must enter a number')
			// everything but letters and numbers are removed from upc and sku
			->add(new Q_Rule_RegExp(array('optional' => true, 'pattern' => '/^[a-z0-9 -]+$/i')), 'sku', 'SKU numbers should only have letters, numbers, spaces and dashes')
			// I'm pretty sure all UPCs are twelve digits
			->add(new Q_Rule_Length(array('optional' => true, 'min' => 12, 'max' => 12)), 'upc', 'A UPC must be exactly 12 digits.')
			->add(new Q_Rule_Digits(array('optional' => true)), 'upc', 'A UPC must be exactly 12 digits.');
		return $rules;
	
	}
	
	public function getListForAdmin($order = null, $direction = null) {
	
		$order = ($order) ? $order : "title";
		$direction = ($direction) ? $direction : "asc";
		$select = $this->select()->where("1 = 1")->order("$order $direction");
		return $select;
	
	}
	
	public function fetchBySlug($slug) {
	
		$select = $this->select()->where('slug = ?', $slug);
		return $this->fetchRow($select);
	
	}

}